package com.nokia.ion.sdu.cdr_decoder;

import java.io.IOException;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created by vicwang on 2017/9/6.
 */
public abstract class CdrWriter implements Runnable {
    private LinkedBlockingQueue<Cdr> queue;

    protected Configuration configuration;

    public CdrWriter(LinkedBlockingQueue<Cdr> queue, Configuration configuration) throws IOException {
        this.queue = queue;
        this.configuration = configuration;
    }

    public void run() {
        Cdr cdr;
        try {
            while (true) {
                cdr = queue.take();
                if (cdr.last) {
                    break;
                }
                write(cdr);
            }
        } catch (InterruptedException e) {
            System.err.println(e);
            System.exit(-1);
        }
        close();
        LoopPrint.run = false;
        System.out.println("CdrWriter thread exit");
    }

    public abstract void write(Cdr cdr) throws InterruptedException;

    abstract void close();
}
